<?php
header("content-type:text/html;charset=utf-8");

session_start();

if(isset($_SESSION["u_user"])) {
	if($_SESSION["u_user"]=='admin') {
	    header("location:admin.php?id=".$_SESSION["u_id"].""); 
	} else {
	    header("location:time.php?id=".$_SESSION["u_id"].""); 
	}
} 
if(isset($_POST["submit"])) {
    if($_POST['username']=='admin' || $_POST['username']=='') {
	    echo '<script>alert("非法用户名");location.href="sign_up.php"</script>';
	}
	if($_POST['username']==''|| $_POST['password']=='' || $_POST['depart']=='' ) {
	    echo '<script>alert("注册信息不能为空！");location.href="sign_up.php"</script>';
	} else {
		include_once('classes/user/user.class.php');
		$user = new user();
        $result = $user->select_user_exist($_POST['username'],'u_user');
		if(!$result) {
			if($user->add_user($_POST['username'],$_POST['password'],$_POST['depart'])) {
		        echo '<script>alert("注册成功");location.href="login.php"</script>';
		        } else {
			        echo '<script>alert("注册失败");location.href="sign_up.php"</script>';
		        }
			} else {
			    echo '<script>alert("用户已存在");location.href="sign_up.php"</script>';
			}
		} 
		
	}	
else {
    include_once('classes/depart/depart.class.php');
    $depart = new depart();
    $info = $depart->get_all_depart();
}

?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>加班管理系统</title>
        <link rel="stylesheet" type="text/css" href="css/styles.css">
        <!--[if IE]>
        <script src="http://libs.baidu.com/html5shiv/3.7/html5shiv.min.js"></script>
        <![endif]-->
    </head>
    <body>
    <div class="container" style="padding-top:100px">
        <div class="login-wrap">
            <div class="login-html">
                <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab">注册</label>
                <div class="login-form">
                <form id="form" name="form" action="sign_up.php" method="post" class="group">
                        <div class="sign-up-html">
                            <div class="group">
                                <label for="username" class="label">用户名</label>
                                <input id="username" name="username" type="text" class="input" placeholder="username">
                            </div>
                            <div class="group">
                                <label for="password" class="label">密码</label>
                                <input id="password" name="password" type="password" class="input" data-type="password" placeholder="password">
                            </div>
                            <div class="group">
                                <label  for="depart" class="label">所属部门</label>
                                <select class="input" id="depart" name="depart" type="text" placeholder="depart">
                                    <?php for($i=0;$i<count($info);$i++){?>
                                    <option value="<?php echo $info[$i]['d_name']?>"><?php echo $info[$i]['d_name']?></option>
                                    <?php }?>
                                </select>
                            </div>
                            <div class="group">
                                <button type="submit" name="submit" id="submit" class="button" >注册</button>
                            </div>
                            <div class="hr"></div>
                            <div class="foot-lnk">
                                <a href="login.php">已有账号？</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    </body>

</html>
